/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.supplement;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.lxk.service.oms.api.OrderNotifyServiceApi;
import com.lxk.service.pay.api.PayServiceApi;
import com.lxk.service.pay.param.OrderQueryParam;
import com.lxk.service.pay.response.OrderQueryResultResponse;
import com.lxk.service.setting.api.SettingServiceApi;
import com.lxk.service.setting.response.ChannelWXPayDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 便利列表，查询微信支付中订单状态
 *
 * @author huahua
 * 2024/8/29
 */
@LiteflowComponent(value = FlowPaySupplementNodeConst.QUERY_WX_ORDER_NODE)
@AllArgsConstructor
public class QueryWxOrderNode extends NodeComponent {
    private final OrderNotifyServiceApi orderNotifyServiceApi;
    private final PayServiceApi payServiceApi;
    private final IOmsOrderService omsOrderService;
    private final SettingServiceApi settingServiceApi;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);
    private final ScheduledExecutorService scheduledExecutorService;

    @Override
    public void process() throws Exception {
        PaySupplementContext contextBean = getContextBean(PaySupplementContext.class);
        //查询支付中-微信
        List<OmsOrderVo> list = contextBean.getWxPayingOrderList();
        if (ObjectUtil.isNotEmpty(list)) {
            List<ChannelWXPayDto> paymsWxPayConfigVoList = settingServiceApi.wxPayAllList();
            Map<String, String> tanantIdAppIdMap = new HashMap<>(paymsWxPayConfigVoList.size());
            for (ChannelWXPayDto payProperties : paymsWxPayConfigVoList) {
                if (StrUtil.isBlank(payProperties.getAppId())) {
                    continue;
                }
                if (StrUtil.isNotBlank(payProperties.getTenantId())) {
                    continue;
                }
                tanantIdAppIdMap.put(payProperties.getTenantId(), payProperties.getAppId());
            }
            for (OmsOrderVo omsOrder : list) {
                OmsOrderVo order = omsOrderService.queryById(omsOrder.getId());
                if (OrderEnums.PayStatus.YES.getCode().equals(order.getPayStatus())) {
                    continue;
                }
                scheduledExecutorService.execute(() -> {
                    OrderQueryParam param = new OrderQueryParam();
                    param.setPayType(omsOrder.getPayType());
                    param.setOutTradeNo(omsOrder.getPayOrderSn());
                    String appId = tanantIdAppIdMap.get(order.getTenantId());
                    if (StrUtil.isNotBlank(param.getAppId())) {
                        param.setAppId(appId);
                    }
                    OrderQueryResultResponse response = payServiceApi.orderQuery(param);
                    log.info("查询微信订单支付结果：{}", response);
                    if (response.getSuccess()) {
                        orderNotifyServiceApi.notifySuccess(omsOrder.getOrderNo(), omsOrder.getPayOrderSn(), omsOrder.getPayOrderSn());
                    }
                });
            }
        }
    }
}
